import { NTimePicker } from "naive-ui";
import type { TimePickerProps, TimePickerInst } from "naive-ui";
import { getSlotsDom } from "../utils";
import type { BaseComponentType, OmitHTMLAttributes } from "../types";

export type TimePickerFactoryConstructorType = {
  props?: TimePickerProps;
  attrs?: OmitHTMLAttributes<"onChange">;
  iconSlot?: BaseComponentType;
};

export class TimePickerFactory {
  private attrs: OmitHTMLAttributes<"onChange"> = {};
  private props: TimePickerProps = {};
  private iconSlot: BaseComponentType = null;
  private domRef: TimePickerInst | null = null;

  constructor(data?: TimePickerFactoryConstructorType) {
    if (data?.props) this.setProps(data.props);
    if (data?.attrs) this.setAttrs(data.attrs);
    if (data?.iconSlot) this.setIcon(data.iconSlot);
  }

  setAttrs(attrs: OmitHTMLAttributes<"onChange">) {
    this.attrs = attrs;
    return this;
  }

  setProps(props: TimePickerProps) {
    this.props = props;
    return this;
  }

  setIcon(component: BaseComponentType) {
    this.iconSlot = component;
    return this;
  }

  focus() {
    this.domRef?.focus();
  }

  blur() {
    this.domRef?.blur();
  }

  create() {
    return (
      <NTimePicker
        ref={(el: HTMLElement) => {
          this.domRef = el as unknown as TimePickerInst;
        }}
        {...this.attrs}
        {...this.props}
      >
        {{
          icon: () => getSlotsDom(this.iconSlot),
        }}
      </NTimePicker>
    );
  }
}
